﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TruthTree2.PC.Logic
{
    public class Decomposition
    {
        private List<Sentence> left;
        private List<Sentence> right;

        public bool HasLeft { get { return left.Count > 0; } }
        public bool HasRight { get { return right.Count > 0; } }

        public Decomposition()
        {
            left = new List<Sentence>();
            right = new List<Sentence>();
        }

        public void AddToLeft(Sentence s) { left.Add(s); }
        public void AddToRight(Sentence s) { right.Add(s); }

        public Tree GetLeftTree()
        {
            if (left.Count < 1) { return null; }
            return new Tree(left);
        }

        public Tree GetRightTree()
        {
            if (right.Count < 1) { return null; }
            return new Tree(right);
        }

        private bool setEquals(List<Sentence> a, List<Sentence> b)
        {
            if (a.Count != b.Count) { return false; }
            Dictionary<Sentence, int> dict = new Dictionary<Sentence, int>();

            foreach (Sentence s in a)
            {
                if (!dict.ContainsKey(s)) { dict[s] = 1; }
                else { dict[s]++; }
            }

            foreach (Sentence s in b)
            {
                if (!dict.ContainsKey(s)) { return false; }
                else { dict[s]--; }
            }

            foreach (int count in dict.Values)
            {
                if (count != 0) { return false; }
            }

            return true;
        }

        public bool Equal(Decomposition o)
        {
            return (setEquals(left, o.left) && setEquals(right, o.right)) ||
                   (setEquals(left, o.right) && setEquals(right, o.left));
        }
    }
}
